<template>
    <div class="goods_list">

        <!-- 路由跳转的两种方式：
        1.a标签跳转
        2.window.location.href 的形式，叫做变成式导航 -->
        <div class="goods_item" v-for="item in goodslist" :key="item.id" @click="goDetail(item.id)">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">${{item.sell_price}}</span>
                    <span class="old">${{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
export default {
    data(){
        return{
            pageindex:1,
            goodslist:[]
        }
    },
    created(){
        this.getGoodsList();
    },
    methods:{
        goDetail(id){
            // 使用js的形式进行路由导航
            // 注意：区分 this.$route 和 this.$router
            // this.$route 是路由参数对象 所有路由中的参数 parmas，query 都属于它
            // this.$router 是路由导航对象，使用js代码，实现路由挑转
            // 多种参数模式，详细请看：https://router.vuejs.org/zh/
            this.$router.push("/home/goodsinfo/"+id);
        },
        getGoodsList(){
            //api 接口数据
            // this.$http.get("api/getgoods?pageindex="+this.pageindex).then(result=>{
            //     if(result.body.status===0){
            //         this.goodslist=result.body.message
            //     }
            // })
            this.goodslist=[
                 {
                    id:1,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i6/TB1I0tWlCzqK1RjSZFpx_mkSXXa_103843.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:2,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i6/TB1I0tWlCzqK1RjSZFpx_mkSXXa_103843.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:3,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i8/TB1mmO5MMHqK1RjSZFgC.17JXXa_122011.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:4,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i3/3367335567/O1CN011AvAVR1qzjTaCTSXV_!!3367335567.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:5,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i5/TB1drQTq.Y1gK0jSZFCtHgwqXXa_033840.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                }
            ]
        },
        getMore(){
            // this.pageindex++;
            // this.getGoodsList();

            this.goodslist.push(
                 {
                    id:this.goodslist.length+1,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i3/3367335567/O1CN011AvAVR1qzjTaCTSXV_!!3367335567.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:this.goodslist.length+2,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i3/3367335567/O1CN011AvAVR1qzjTaCTSXV_!!3367335567.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:this.goodslist.length+3,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i3/3367335567/O1CN011AvAVR1qzjTaCTSXV_!!3367335567.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                },
                 {
                    id:this.goodslist.length+4,
                    title:"Samsung/三星 Galaxy Note10+ SM-N9760 5G",
                    add_time:Date.now(),
                    zhaiyao:"朵唯官方旗舰店",
                    click:99,
                    img_url:"https://img.alicdn.com/bao/uploaded/i5/TB1drQTq.Y1gK0jSZFCtHgwqXXa_033840.jpg",
                    sell_price:"499",
                    market_price:"600",
                    stock_quantity:60
                }
            )
            // this.goodslist.forEach(item=>{
            //     console.log(item.id);
            // })
        }
    }
}
</script>

<style lang="scss" scoped>
.goods_list{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 8px;
    .goods_item{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 8px #ccc;
        margin: 3px 0;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 290px;
        img{
            width: 100%;
        }
        .title{
            font-size: 14px;
        }
        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .price{
                .now{
                    color: red;
                    font-weight: bold;
                    font-size: 16px;
                }
                .old{
                    text-decoration: line-through;
                    font-style: 12px;
                    margin-left: 10px;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
                font-style: 12px;
            }

        }
    }
}
</style>